

package orest.back.unit;
import orest.common.response.Response;
import orest.common.valueobject.UnitVal;
import orest.common.dao.UnitDao;
import java.util.*;
import orest.common.util.*;
/**
 *
 * @author admin
 */
public class UnitAction {

     public UnitAction() {
    }

  

      public boolean setChannelShow(String unitid,String ifshow) {
    	UnitDao obj = new UnitDao();
        boolean exe = true;
        exe = obj.setChannelShow (unitid,ifshow);
    	return exe;
    }


       public boolean SetChannelShow_Batch(String ifshow,String[] id) {
    	UnitDao obj = new UnitDao();
    	return obj.SetChannelShow_Batch(ifshow,id);
    }

    

     


      public String findUnitByCondition(String key, int pageNo,
                                                      int pageRecords, long ttl) {

        Response            rp       = new Response();
        StringBuffer        result   = new StringBuffer();
        UnitDao        obj      = new UnitDao();
        GetOption    objem     =new  GetOption();
        boolean            opReturn = true;
        String              temp     = "";
        long                total    =0;



        result.append ("<?xml version='1.0' encoding='GBK'?>");
        result.append ("<root>");

        /**
         * execute query and get data that were packed in entity bean and
         * saved in arraylist object.
         */



        try{
            if(pageNo <= 1 || (pageNo > 1 && ttl == 0))
            {
               total = obj.countUnitByCondition(key,rp);
            }
            else
                total = ttl;
            opReturn = obj.findUnitShowByConditon(rp,key,pageNo, pageRecords,(int)total) ;
            if(!opReturn)
                return null;
        }
        catch(Exception e) {
            e.printStackTrace();
        }


        result.append("<total>");
        result.append(total);
        result.append("</total>");

    
        ArrayList al = rp.getValObjs ();
        Iterator iterator = al.iterator ();
        try{
            while(iterator.hasNext ()){
                UnitVal objElem = (UnitVal)iterator.next();
                
                result.append ("<record>");

             

                result.append ("<UnitID>");
                result.append (objElem.getUnitID());
                result.append ("</UnitID>");

                result.append ("<Item_9><![CDATA[");
                result.append (objElem.getItem_9().trim());
                result.append ("]]></Item_9>");

                result.append ("<NewEnergyClass1>");
                result.append (objElem.getNewEnergyClass1());
                result.append ("</NewEnergyClass1>");

                temp = objem. getOptionNameFromOptionValue("common","NewEnergyClass",objElem.getNewEnergyClass1());

                if (temp !=null) {
                    result.append ("<NewEnergyClassName1>");
                    result.append (temp);
                    result.append ("</NewEnergyClassName1>");
                }

                result.append ("<NewEnergyClass2>");
                result.append (objElem.getNewEnergyClass2());
                result.append ("</NewEnergyClass2>");

                temp = objem. getOptionNameFromOptionValue("common","NewEnergyClass",objElem.getNewEnergyClass2());

                if (temp !=null) {
                    result.append ("<NewEnergyClassName2>");
                    result.append (temp);
                    result.append ("</NewEnergyClassName2>");
                }
                result.append ("<Item_64>");
                result.append (objElem.getItem_64());
                result.append ("</Item_64>");

                 if(objElem.getItem_64() != null) {
                    temp = objem.getProvinceNameFromProvinceID(objElem.getItem_64());
                    if(null != temp) {
                        result.append ("<ProvinceName>");
                        result.append(temp);
                        result.append ("</ProvinceName>");
                }
                }

                result.append ("<ChannelShow>");
                result.append (objElem.getChannelShow());
                result.append ("</ChannelShow>");
                result.append ("<HomepageShow>");
                result.append (objElem.getHomepageShow());
                result.append ("</HomepageShow>");
                             
                result.append ("</record>");
                objElem = null;
            }
        }
        catch(Exception e) {
            System.out.println ("when encoding the string, exception occured.");
            System.out.println ("The class is UnitAction and the function is " +
                                 "findUnitByCondition");
        }

        result.append ("</root>");

        al = null;
        obj = null;
        rp = null;
   

        return result.toString ();

    }

   

     

   

}
